package com.unidt.www.module.system.dal.mysql.organ;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.unidt.www.module.system.dal.dataobject.organ.OrganCodeDo;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Mapper
public interface OrganCodeMapper extends BaseMapper<OrganCodeDo> {

    /**
     * 功能描述: 查询当前区下级
     * @author sk
     * @date 2022/12/8
     * @param organId
     * @return java.util.List<com.unidt.www.module.system.dal.dataobject.organ.OrganCodeDo>
     */
    default List<OrganCodeDo> getChildren(Long organId){
        LambdaQueryWrapper<OrganCodeDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrganCodeDo::getParentId,organId);
        lambdaQueryWrapper.orderByAsc(OrganCodeDo::getId);
        return selectList(lambdaQueryWrapper);

    };

    default List<OrganCodeDo> getAll(Long organId){
        LambdaQueryWrapper<OrganCodeDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(OrganCodeDo::getId,organId);
        lambdaQueryWrapper.orderByAsc(OrganCodeDo::getId);
        return selectList(lambdaQueryWrapper);
    };

    default List<OrganCodeDo> getAll(Integer level){
        LambdaQueryWrapper<OrganCodeDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.le(OrganCodeDo::getLevel,level);
        lambdaQueryWrapper.orderByAsc(OrganCodeDo::getId);
        return this.selectList(lambdaQueryWrapper);

    }


    default List<OrganCodeDo> getByIds(List<Long> organIds){
        if(organIds==null || organIds.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<OrganCodeDo> lambdaQueryWrapper =
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(OrganCodeDo::getId,organIds);
        lambdaQueryWrapper.orderByAsc(OrganCodeDo::getId);
        return this.selectList(lambdaQueryWrapper);

    }


    default Map<Long,OrganCodeDo> getMapByIds(List<Long> organIds){
        if(organIds!=null && organIds.isEmpty()){
            LambdaQueryWrapper<OrganCodeDo> lambdaQueryWrapper =
                    new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(OrganCodeDo::getId,organIds);
            lambdaQueryWrapper.orderByAsc(OrganCodeDo::getId);
            List<OrganCodeDo> organCodeDoList =  this.selectList(lambdaQueryWrapper);
            if(organCodeDoList.isEmpty()){
                return organCodeDoList.stream().collect(Collectors.toMap(OrganCodeDo::getId,a->a,(a1,a2)->a1));
            }
        }

        return new HashMap<>();
    }


}
